package com.linck.im.parse.decode;

import com.linck.im.model.proto.ProtoMsg;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;

import java.util.List;

/**
 * 根据type从消息包中获取荷载内容
 *
 * @author: linck
 * @create: 2021-01-26
 */
public class PackageDecode extends MessageToMessageDecoder<ProtoMsg.Package> {

    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ProtoMsg.Package pack, List<Object> out) throws Exception {
        if (pack != null) {
            // 获取业务消息头，取出具体消息类型
            if (pack.getMsgType().equals(ProtoMsg.MsgType.MESSAGE)) {
                out.add(pack.getMessage());
            } else if (pack.getMsgType().equals(ProtoMsg.MsgType.LOGIN_INFO)) {
                out.add(pack.getLoginInfo());
            } else if (pack.getMsgType().equals(ProtoMsg.MsgType.SYS_MSG)) {
                out.add(pack.getSysMsg());
            } else if (pack.getMsgType().equals(ProtoMsg.MsgType.FRIENDS)) {
                out.add(pack.getFriends());
            } else if (pack.getMsgType().equals(ProtoMsg.MsgType.HEART_BEAT)) {
                out.add(pack.getHeartBeat());
            }
        }
    }
}